
package com.example.demo;

import com.example.demo.config.cos.CosConfigProperties;
import com.example.demo.config.cos.UploadManagers;
import com.google.gson.Gson;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.mail.MailException;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.test.context.junit4.SpringRunner;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Map;
import java.util.concurrent.TimeUnit;


/**
 * @author :xie
 * Email: 1487471733@qq.com
 * Date: 2018/1/20
 * Time: 16:14
 * Describe:
 */

@RunWith(SpringRunner.class)
@SpringBootTest
public class demo {

    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;
    @Autowired
    StringRedisTemplate template;
    @Autowired
    private JavaMailSender mailSender;

    @Autowired
    private COSClient cosClient;

    @Autowired
    private Auth auth;

    @Autowired
    private UploadManager uploadManager;


    @Test
    public void test1(){
        //template.opsForValue().set("name","tom");
        //template.opsForValue().get("name");
        System.out.println( template.opsForValue().get("name")+template.opsForValue().get("xiaoyanifno"));

    }

    @Test
    public void mail(){
        String mail="1487471733@qq.com";
        int code=0;

        if(template.opsForValue().get(mail)!=null){

            code= Integer.parseInt(template.opsForValue().get(mail));
        }else{
            code = (int)((Math.random()*9+1)*100000);
        }
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        try {
            // 指明邮件的发件人
            mimeMessage.setFrom(new InternetAddress("xiaoyaninfo@xiaoyaninfo.xyz"));
            //指明邮件的收件人，现在发件人和收件人是一样的，那就是自己给自己发
            mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(mail));
            //邮件的标题
            mimeMessage.setSubject("注册验证");
            //邮件的文本内容
            mimeMessage.setContent("<html><head><title>编辑邮件正文</title><style>html{word-wrap:break-word;}body{color:#000000;font-size:14px;font-family:Arial;line-height:1.7;padding:8px 10px;margin:0;background-color:#fff;}pre{white-space:pre-wrap;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;word-wrap:break-word;font-family:arial;}span.typoRed{border-bottom:2px dotted #ff0000;cursor:pointer;}.nui-scroll{overflow:auto}.nui-scroll::-webkit-scrollbar{width: 8px;height:8px}.nui-scroll::-webkit-scrollbar-track{border-radius:10px;-webkit-box-shadow:inset 0 0 6px rgba(0,0,0,0);}.nui-scroll::-webkit-scrollbar-track:hover{-webkit-box-shadow:inset 0 0 6px rgba(0,0,0,0.4);background-color:rgba(0,0,0,0.01)}.nui-scroll::-webkit-scrollbar-track:active{-webkit-box-shadow:inset 0 0 6px rgba(0,0,0,0.4);background-color:rgba(0,0,0,0.05)}.nui-scroll::-webkit-scrollbar-thumb{background-color: rgba(0, 0, 0, 0.05);border-radius:10px;-webkit-box-shadow:inset 1px 1px 0 rgba(0,0,0,.1)}.nui-scroll:hover::-webkit-scrollbar-thumb{background-color: rgba(0, 0, 0, 0.2);border-radius:10px;-webkit-box-shadow:inset 1px 1px 0 rgba(0,0,0,.1)}.nui-scroll::-webkit-scrollbar-thumb:hover{background-color:rgba(0, 0, 0, 0.4);-webkit-box-shadow:inset 1px 1px 0 rgba(0,0,0,.1)}.nui-scroll::-webkit-scrollbar-thumb:active{background:rgba(0,0,0,0.6);}</style></head><body class=\"nui-scroll\" contenteditable=\"true\"><div style=\"text-align: center;\"><span style=\"font-size: 24px;\"><b>注册验证</b></span></div><div style=\"text-align: left;\"><span style=\"font-size: 18px;\">\u200B欢迎注册 验证码:&nbsp;"+code+"   验证码有效期为十分钟</span></div></body></html>", "text/html;charset=UTF-8");
        } catch (MessagingException e) {
            e.printStackTrace();
        }


        /*message.setFrom("fanshuye1304@163.com");//发送者.
        message.setTo(mail); // 接收者.
        message.setSubject("Bing-Upms 注册验证"); // 邮件主题.
        message.setText("<h1>验证码</h1>"); // 邮件内容.*/

        try {
            mailSender.send(mimeMessage); //发送邮件
            template.opsForValue().set(mail,code+"",10*60, TimeUnit.SECONDS);
            System.out.println("请查看邮箱验证码");
        } catch (MailException e) {
            System.out.println("邮箱发送失败,请重新获取");
        }
    }


    @Test
    public void upload(){

        File localFile = new File("F:/IMG_6078.JPG");
        String key = "/upload_single_demo.JPG";
        String bucketName = "xioyan-1251811015";
        System.out.println(cosClient.getClientConfig().getRegion());

        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
        PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
       System.out.println(putObjectResult);

/*        Map<String, String> cosMap = JsonMapUtil.parseToMap(result);
        Map<String, String> cosData = JsonMapUtil.parseToMap(cosMap.get("data"));*/
    }

    @Test
    public  void upload_qiniu() {
        File localFile = new File("F:/IMG_6078.JPG");
       String bucketName = "xioyan";

        String upToken= auth.uploadToken(bucketName);

        /*  Configuration cfg = new Configuration(Zone.zone2());
        UploadManager uploadManager = new UploadManager(cfg);*/

        try {
            Response response = uploadManager.put(localFile, null, upToken);
            //解析上传成功的结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            System.out.println(putRet.key);
            System.out.println(putRet.hash);
        } catch (QiniuException ex) {
            Response r = ex.response;
            System.err.println(r.toString());
            try {
                System.err.println(r.bodyString());
            } catch (QiniuException ex2) {
                //ignore
            }
        }

    }
}

